<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>停止事件的冒泡</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box1 {
            width: 200px;
            height: 200px;
            background-color: red;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: green;
        }
    </style>
</head>
<body>
<div id="change" class="change">
    <div class="box1" data-on:click="box1clicked">
        <!--.stop 修饰符 用来阻止事件的冒泡-->
        <div class="box2" data-on:click.stop="box2clicked"></div>
    </div>
</div>

<script>
    let outer = document.querySelector("#change .box1");
    let inner = document.querySelector("#change .box2");

    function box1clicked() {
        console.log("点击了 外部区域");
    }

    function box2clicked() {
        console.log("点击了 内部区域");
    }

    function event_handling(element) {
        // 获取所有的属性
        let attributeNames = element.getAttributeNames();
        // console.log(attributeNames);

        for (let i = 0; i < attributeNames.length; i++) {
            // console.log(attributeNames[i]);

            if (attributeNames[i].indexOf("data-on:") === 0 || attributeNames[i].indexOf("@") === 0) {
                let start = null
                let event_name = null
                let modifier = null
                let event_code = null

                if (attributeNames[i].indexOf("data-on:") === 0){
                    start = attributeNames[i].indexOf(":")
                }
                else {
                    start = attributeNames[i].indexOf("@")
                }

                if (attributeNames[i].indexOf(".") !== -1) {
                    event_name = attributeNames[i].substring(start + 1, attributeNames[i].indexOf("."));
                    modifier = attributeNames[i].substring(attributeNames[i].indexOf(".") + 1)
                }

                else {
                    event_name = attributeNames[i].substring(start + 1)
                }

                event_code = element.getAttribute(attributeNames[i])

                console.log(event_name);
                console.log(event_code);
                console.log(modifier);

                element.addEventListener(event_name, function (event) {
                    if (modifier !== null) {
                        if (modifier === "stop") {
                            event.stopPropagation()
                        }
                    }
                    eval(event_code + "()")
                })

                element.removeAttribute(attributeNames[i])
            }

        }
    }

    event_handling(outer)
    event_handling(inner)

</script>
</body>
</html>